<!--
@description: 邀请加入
@creationTime: 2025-04-22
-->

<route type="page" lang="json5">
{
  layout: 'default',
  style: {
    navigationBarTitleText: '邀请加入'
  }
}
</route>

<template>
  <view class="p-5">
    <view class="mb-8">
      <text class="text-xl font-bold block mb-3">{{ user.companyName }}</text>
      <text class="text-gray-600 block">联系人：{{ user.username }}</text>
      <text class="text-gray-600">通过审核，可加入{{ user.companyName }}</text>
    </view>
    <view class="mt-3">
      <wd-button :block="true" class="bg-blue-500 text-white px-4 py-2 rounded" @click="handleAgree">
        申请加入
      </wd-button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { addBindCompanyApi } from '@/api/userInfoByType'
import { getUserApi } from '@/api/loginApi'
import { useUserStore } from '@/store'

const userStore = useUserStore()
const pageConfig = ref<any>({})
const user = ref<any>({})
onLoad(res => {
  if (!userStore.userInfo.accessToken) {
    uni.showToast({ title: '请先登录', icon: 'none' })
    setTimeout(() => {
      uni.redirectTo({ url: `/pages/login/index?type=${res.type}&id=${res.id}` })
    }, 1000)
    return
  }
  pageConfig.value = res
  getUserApi({ userId: res.id }).then((res: any) => {
    user.value = res.data
  })
})

const handleAgree = () => {
  uni.showLoading({ title: '申请中...' })
  addBindCompanyApi({
    companyUserId: pageConfig.value.id,
    userType: pageConfig.value.type,
    staffUserId: userStore.userInfo.id
  }).then(() => {
    uni.hideLoading()
    uni.showToast({ title: '加入成功', icon: 'success' })
    setTimeout(() => {
      uni.switchTab({ url: '/pages/home/company-home/index' })
    }, 1000)
  })
}
</script>

<style lang="scss" scoped></style>
